﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CtpGglMap.Impl;

namespace CtpGglMap.Plus
{
    public class SimpleCluster: ICluster
    {
        public SimpleCluster()
        {
            this.VisibleItems = new List<IMapItem>();
            this.InvisibleItems = new List<IMapItem>();
        }

        IMap _map = null;

        public IMap Map
        {
            get
            {
                return _map;
            }
            set
            {
                _map = value;
            }
        }

        public string GetShortenUrl()
        {
            if(_map!=null&&
                (_map.GetType().IsSubclassOf(typeof(CtpGglMap.Impl.StaticGMap))|| _map.GetType() == typeof(CtpGglMap.Impl.StaticGMap))
                )
            {
                 StringBuilder sb = new StringBuilder(CtpGglMap.Core.StaticMap.Const.GOOGLE_STATIC_MAPS_API_URL + "?");

                CtpGglMap.Impl.StaticGMap staticMap = (CtpGglMap.Impl.StaticGMap)_map;

                //build up parameters
                //build up parameters
                List<String> param = new List<string>();
                if (this.Map.Center != null)
                {
                    if (!staticMap.AutoFit)
                        param.Add(string.Format("center={0},{1}", Map.Center.Lat, Map.Center.Lng));
                }
                else
                    return null;
                if (!staticMap.AutoFit)
                    param.Add("zoom=" + this.Map.Zoom);
                param.Add(String.Format("size={0}x{1}", staticMap.Width, staticMap.Height));
                if (!String.IsNullOrEmpty(this.Map.MapType))
                    param.Add("maptype=" + this.Map.MapType);
                param.Add("sensor=" + this.Map.Sensor.ToString().ToLower());
                //Stringify. 
                for (int i = 0; i < param.Count ; i++)
                {
                    sb.Append(param[i]);
                    if (i < param.Count - 1)
                        sb.Append("&");
                }
                this.VisibleItems = new List<IMapItem>();
                this.InvisibleItems = new List<IMapItem>();
                foreach (IMapItem item in staticMap.GetItems())
                {
                    if (item != null)
                    {
                        String h = String.Empty;
                        if (item.GetType() == typeof(GMarker) || item.GetType().IsSubclassOf(typeof(GMarker)))
                        {
                            GMarker marker = item as GMarker;
                            h = ("&markers=" + marker.GetUrl());
                        }
                        else if (item.GetType() == typeof(GPath))
                        {
                            GPath path = item as GPath;
                            h = ("&path=" + path.GetUrl());
                        }
                        if (sb.Length + h.Length < CtpGglMap.Core.StaticMap.Const.MAX_REQUEST_CHARACTER_LENGTH)
                        {
                            sb.Append(h);
                            VisibleItems.Add(item);
                        }
                        else
                        {
                            InvisibleItems.Add(item);
                        }
                    }
                }
               
                return sb.ToString();
            }else 
                throw new NotSupportedException("this cluster only supports CtpGglMap.Impl.StaticGMap & its subclass");
        }

        public List<IMapItem> VisibleItems
        {
            get;
            set;
        }

        public List<IMapItem> InvisibleItems
        {
            get;
            set;
        }
    }
}
